﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace CommonUtils
{
    public class ServerList<TClient, TChannel> : SyncedList<TClient>
                    where TChannel: class
                    where TClient : ClientBase<TChannel>  
    {
        /// <summary>
        /// Adds a server to the list. When this method is invoked, a client
        /// is created for the server and is added to the list.
        /// </summary>
        /// <param name="address">end point address of the server being added.</param>
        public static void AddServer(
                                 string binding, 
                                 EndpointAddress address
                                 )
        {
            if (address != null && binding != null)
            {
                object[] clientParams = new object[2]
                                            {
                                                binding,
                                                address
                                            };

                TClient client = (TClient)Activator.CreateInstance(
                                               typeof(TClient), 
                                               clientParams
                                               );
                Add(client);
            }
        }

        /// <summary>
        /// Get a server from the list.
        /// </summary>
        /// <param name="id">Server ID</param>
        /// <returns>A client that can be used to connect to the requested server</returns>
        public static TClient GetServer(int id)
        {
            return GetItem(id);
        }

        /// <summary>
        /// Remove a server from the list.
        /// </summary>
        /// <param name="endpoint">The endpoint of the server being removed.</param>
        public static void RemoveServer(EndpointAddress endpoint)
        {
            TClient target = null;
            foreach (TClient client in Items)
            {
                if (client.Endpoint.Address == endpoint)
                {
                    target = client;
                    break;
                }
            }
            Remove(target);
        }

        public static void CloseAll()
        {
            foreach (TClient client in Items)
            {
                client.Close();
            }
        }

        public static void DebugPrint()
        {
            if (Items == null)
            {
                Console.WriteLine("There are no Back-End servers currently registered.");
                return;
            }
            int i = 1;
            foreach (TClient client in Items)
            {
                Console.WriteLine(
                        "Server {0} URL is: {1}",
                        i++,
                        client.Endpoint.Address.ToString());
            }
            if (i == 1)
            {
                Console.WriteLine("There are no registered BackEnd servers currently registered.");
            }
        }

        public static bool IsEmpty()
        {
            return (Items == null || Items.Count == 0);
        }

        public static int Count
        {
            get 
            { 
                if (Items != null)
                {
                    return Items.Count;
                }
                return 0;
            }
        }
    }
}
